#!/usr/bin/python3
# -*- coding: utf-8; tab-width: 4; indent-tabs-mode: t -*-

import os
import subprocess


fullfnSet = set()

# get files
cmdStr = r"cat /var/db/pkg/*/*/CONTENTS "
cmdStr += r'| sed -e "s:^obj \(.*\) [[:xdigit:]]\+ [[:digit:]]\+$:\1:" '
cmdStr += r'| sed -e "s:^sym \(.*\) -> .* .*$:\1:" '
cmdStr += r'| sed -e "s:^dir \(.*\)$:\1:" '
ret = subprocess.check_output(cmdStr, shell=True, text=True)
fullfnSet = set(ret.split("\n"))

# deal with .keep
nret = set()
for fullfn in fullfnSet:
    if os.path.isdir(fullfn):
        for fn2 in os.listdir(fullfn):
            if fn2 == ".keep" or fn2.startswith(".keep_"):
                nret.add(os.path.join(fullfn, fn2))
                print(os.path.join(fullfn, fn2))
fullfnSet |= nret

# deal with *.py
nret = set()
for f in fullfnSet:
    if f.endswith(".py"):
        if os.path.exists(f + "c"):
            nret.add(f + "c")
        if os.path.exists(f + "o"):
            nret.add(f + "o")
fullfnSet |= nret

# deal with __pycache__
for dn in ["/usr/lib", "/usr/lib64", "/usr/libexec"]:
    ret = subprocess.check_output(["find", dn, "-regex", r'.*/__pycache__\(/.*\)?'], text=True)
    fullfnSet |= set(ret.split("\n"))

# output result
for fullfn in fullfnSet:
    print(fullfn)
